$.extend($.messager.defaults,{  
    ok:"确定",  
    cancel:"取消"  
});  

var url;
var editCellIndex = undefined;



function openNewDlg(title,actUrl,dlgId,fmId){
	$("#"+dlgId).dialog('open').dialog('setTitle',title);
	if(fmId){
		$("#"+fmId).form('clear');
	}	
    url= $("#base").val()+"/"+actUrl;
}

function submit(dlgId, fmId) {
	var base=$("#base").val();	
	$("#" + fmId).form('submit', {
		url : url,
		onSubmit : function() {
			return $(this).form('validate');
		},
		success : function(result) {
			var result = eval('(' + result + ')');
			$("#" + dlgId).dialog('close');
			if (result.success) {
				if(result.msg != null && result.msg !=''){
					$.messager.alert('提示', result.msg, 'info');
				}			
				$('#datagrid').datagrid('reload');
			} else {
				if (result.msg) {
					$.messager.alert('错误', result.msg, 'error');
				} else if (result.infoMsg) {
					$.messager.alert('提示', result.infoMsg, 'info');
				} else {
					$('#datagrid').datagrid('reload');
				}
			}
		}
	});
}

function openEditDlg(title,actUrl,dlgId,fmId){
    var rows = $('#datagrid').datagrid('getSelections');
    if (rows&&rows.length==1){
    	$("#"+dlgId).dialog('open').dialog('setTitle',title);
    	$("#"+fmId).form('clear');
    	$("#"+fmId).form('load',rows[0]);
        url = $("#base").val()+"/"+actUrl;
        if(rows[0].id){
        	url+='?id='+rows[0].id;
        }else if(rows[0].acl){
        	url+='?acl='+rows[0].acl;
        }else{
        	url+='?uid='+rows[0].uid;
        }
    }else{
    	$.messager.alert('提示','请选择一条数据进行操作','info');
    }
}

function loadFormData(title,submitUrl,loadUrl,dlgId,fmId){
    var rows = $('#datagrid').datagrid('getSelections');
    if (rows&&rows.length==1){
    	$("#"+dlgId).dialog('open').dialog('setTitle',title);
    	$("#"+fmId).form('clear');
    	$("#"+fmId).form('load',$("#base").val()+"/"+loadUrl+'?id='+rows[0].id);
        url = $("#base").val()+"/"+submitUrl;
        if(rows[0].id){
        	url+='?id='+rows[0].id;
        }else if(rows[0].acl){
        	url+='?acl='+rows[0].acl;
        }else{
        	url+='?uid='+rows[0].uid;
        }
    }else{
    	$.messager.alert('提示','请选择一条数据进行操作','info');
    }
}

function multiDel(actUrl){
	var rows = $('#datagrid').datagrid('getSelections');
    if (rows.length>0){
    	var ids = [];
    	for(var i=0; i<rows.length; i++){
    		ids.push(rows[i].id);
    	}
    	url = $("#base").val()+"/"+actUrl+'?ids='+ids.join(",");
        $.messager.confirm('删除','确定要删除吗?',function(r){
            if (r){
                $.post(url,function(result){
                	var result = eval('('+result+')');
                	if (result.errorMsg){
                 	   $.messager.alert('错误',result.errorMsg,'error');
                    } else if(result.infoMsg){
                 	   $.messager.alert('提示',result.infoMsg,'info');
                    }else{
                       $('#datagrid').datagrid('reload');   
                    }
                });
            }
        });
    }
}


function singleDel(actUrl){
	var row = $('#datagrid').datagrid('getSelected');
    if (row){
    	url = $("#base").val()+"/"+actUrl;
    	if(row.id){
        	url+='?id='+row.id;
        }else{
        	url+='?acl='+row.acl;
        }
        $.messager.confirm('删除','确定要删除吗?',function(r){
            if (r){
                $.post(url,function(result){
                	var result = eval('('+result+')');
                	if (result.errorMsg){
                 	   $.messager.alert('错误',result.errorMsg,'error');
                    } else if(result.infoMsg){
                 	   $.messager.alert('提示',result.infoMsg,'info');
                    }else{
                       $('#datagrid').datagrid('reload');   
                    }
                });
            }
        });
    }
   }

function multiUpdateSorts(actUrl){
	$('#datagrid').datagrid('endEdit', editCellIndex);
	var rows = $('#datagrid').datagrid('getChanges');
    if (rows.length>0){
    	var ids = [];
    	var sorts = [];
    	for(var i=0; i<rows.length; i++){
    		ids.push(rows[i].id);
    		var hasSortNo=rows[i].sortNo||rows[i].sequence;
    		if(!hasSortNo){
    			$.messager.alert('提示','序号不能为空','info');
    			return;
    		}
    		sorts.push(hasSortNo);
    	}
    	url = $("#base").val()+"/"+actUrl+'?ids='+ids.join(",")+'&sorts='+sorts.join(",");
                $.post(url,function(result){
                  $('#datagrid').datagrid('reload');   
        });
    }
}


function back(backUrl){
	var actUrl = $("#base").val()+"/"+backUrl;
	$('#mainPage').panel('open').panel('refresh',actUrl);
}

$.extend($.fn.datagrid.methods, {
	editCell: function(jq,param){
		return jq.each(function(){
			var opts = $(this).datagrid('options');
			var fields = $(this).datagrid('getColumnFields',true).concat($(this).datagrid('getColumnFields'));
			for(var i=0; i<fields.length; i++){
				var col = $(this).datagrid('getColumnOption', fields[i]);
				col.editor1 = col.editor;
				if (fields[i] != param.field){
					col.editor = null;
				}
			}
			$(this).datagrid('beginEdit', param.index);
			for(var i=0; i<fields.length; i++){
				var col = $(this).datagrid('getColumnOption', fields[i]);
				col.editor = col.editor1;
			}
		});
	}
});

function endEditCell(){
	if (editCellIndex == undefined){return true}
	if ($('#datagrid').datagrid('validateRow', editCellIndex)){
		$('#datagrid').datagrid('endEdit', editCellIndex);
		editCellIndex = undefined;
		return true;
	} else {
		return false;
	}
}
function onClickCell(index, field){
	if (endEditCell()){
		$('#datagrid').datagrid('selectRow', index).datagrid('editCell', {index:index,field:field});
	}
	editCellIndex=index;
}

function myformatter(date){
    var y = date.getFullYear();
    var m = date.getMonth()+1;
    var d = date.getDate();
    return y+'-'+(m<10?('0'+m):m)+'-'+(d<10?('0'+d):d);
}
function myparser(s){
    if (!s) return new Date();
    var ss = (s.split('-'));
    var y = parseInt(ss[0],10);
    var m = parseInt(ss[1],10);
    var d = parseInt(ss[2],10);
    if (!isNaN(y) && !isNaN(m) && !isNaN(d)){
        return new Date(y,m-1,d);
    } else {
        return new Date();
    }
}

function isEmpty(str){  
    return str == null || !str || typeof str == undefined || str == '';
} 



